package ro.msg.quiz.controller;

import java.util.ArrayList;
import java.util.List;

import ro.msg.quiz.domain.Question;
import ro.msg.quiz.domain.QuestionType;
import ro.msg.quiz.domain.Quiz;
import ro.msg.quiz.repository.Repository;

//AUTHORS: Ervin, Marco

//Class responsible for converting data from repository to match the required data of GUI classes
public class GuiService {

    private Repository rep = Repository.getInstance();
    private List<Quiz> quizzesList = new ArrayList<Quiz>();
    private List<Question> questionsList = new ArrayList<Question>();

    public GuiService() {
	this.quizzesList = rep.getQuizzesList();
	this.questionsList = rep.getQuestionsList();
    }

    // returns a matrix of Strings containing 3 columns |quiz name|nr of
    // questions|last modified| on each row
    public String[][] getQuizzesAndNrOfQuestions() {
	String[][] matrixString = new String[quizzesList.size()][3];
	int i = 0;
	for (Quiz quiz : this.quizzesList) {
	    matrixString[i][0] = quiz.getName();
	    matrixString[i][1] = String.valueOf(quiz.getNrOfQuestions());
	    matrixString[i][2] = "Last modified unknown";
	    i++;
	}

	return matrixString;
    }

    // returns a matrix of Strings containing 3 columns |question text|question
    // type|last modified| on each row
    public String[][] getQuestionsAndType() {
	String[][] matrixString = new String[questionsList.size()][3];
	int i = 0;
	for (Question question : questionsList) {
	    matrixString[i][0] = question.getText();
	    String type = "Unknown type";

	    if (question.getType().equals(QuestionType.SINGLE_CHOICE_Q))
		type = "Single Choice";
	    else if (question.getType().equals(QuestionType.MULTIPLE_CHOICE_Q))
		type = "Multiple choice";
	    else
		type = "Text answer";

	    matrixString[i][1] = type;
	    matrixString[i][2] = "Last modified unknown";
	    i++;
	}

	return matrixString;
    }

    public String[] getQuestions() {
	String[] s = new String[questionsList.size()];

	int i = 0;
	for (Question question : questionsList)
	    s[i++] = question.getText();

	return s;
    }

    public void editQuiz(String[] s, int selectedRow) {
	quizzesList.get(selectedRow).setName(s[0]);

    }

    public void editQuestion(String[] s, int selectedRow) {
	questionsList.get(selectedRow).setText(s[0]);
    }

    public Repository getRep() {
	return rep;
    }

    public List<Quiz> getQuizzesList() {
	return quizzesList;
    }

    public List<Question> getQuestionsList() {
	return questionsList;
    }
    
}